package main;

import guiobj.CluPanelController;
import guiobj.JobPanelController;

import java.util.LinkedList;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import runnable.GetFiles;

import jsonobj.AttrInfo;
import jsonobj.ClusterIDInfo;
import jsonobj.Co2Info;
import jsonobj.JSonObj;
import jsonobj.Message;
import jsonobj.ResInfo;

import data.WebSocket;

import utils.Msg;
import utils.Utils;

public class ReceiveThread extends Thread implements JSonObj, Msg {
	public static volatile boolean run = true;
	private WebSocket ws;

	public ReceiveThread() {
		this.ws = Utils.ws;
	}

	public void run() {
		while (run) {
			try {
				String data = ws.recv();

				String[] tokens = data.split("\\|");
				byte obj_type = Byte.parseByte(tokens[0]);
				String json = tokens[1];

				if (obj_type == PING) {
					Utils.ui.cinfo = new Gson().fromJson(json,
							new TypeToken<LinkedList<ClusterIDInfo>>() {
							}.getType());
					for (ClusterIDInfo i : Utils.ui.cinfo)
						CluPanelController.addClusterInfo(Utils.p, i);
					Utils.ws.send("8|0");
					continue;
				}
				Utils.out("RECV: " + data);
				switch (obj_type) {
				case ATTR_INFO: {
					AttrInfo ai = AttrInfo.fromJSon(json);
					JobPanelController.updateJobInfo(ai);
					break;
				}
				case RES_INFO: {
					ResInfo ri = ResInfo.fromJSon(json);
					JobPanelController.updateJobInfo(ri);
					break;
				}
				case CO2_INFO: {
					Co2Info ci = Co2Info.fromJSon(json);
					Utils.out("UPDATING INFO: " + ci);
					JobPanelController.updateJobInfo(ci);
					break;
				}
				case MSG: {
					Message msg = Message.fromJson(json);
					switch (msg.type) {
					case JOB_QUEUED: {
						JobPanelController.updateJobStatus(Utils.p, msg.msg,
								"Queued");
						break;
					}
					case JOB_EXECUTING: {
						JobPanelController.updateJobStatus(Utils.p, msg.msg,
								"Running");
						break;
					}
					case JOB_FAILED: {
						JobPanelController.updateJobStatus(Utils.p, msg.msg,
								"Failed");
						break;
					}
					case JOB_TERMINATED: {
						Utils.exec.execute(new GetFiles(msg.msg));
						break;
					}
					case CLUSTER_ON: {
						if (!CluPanelController.clusters.contains(msg.msg)) {
							if (CluPanelController.clusters.isEmpty())
								Utils.p.cluCombo.removeAllItems();
							Utils.p.cluCombo.addItem(msg.toHost);
						}
						CluPanelController.addClusterInfo(Utils.p, msg.msg);
						Utils.ui.cinfo.add(new ClusterIDInfo(msg.msg,
								msg.toHost));
						break;
					}
					case CLUSTER_OFF: {
						CluPanelController.updateClusterStatus(Utils.p,
								msg.msg, "Disconnected");
						int i = 0;

						while (i < Utils.p.cluCombo.getItemCount()) {
							String item = (String) Utils.p.cluCombo
									.getItemAt(i);
							if (item.equals(msg.toHost)) {
								Utils.p.cluCombo.removeItemAt(i);
								break;
							}
							i++;
						}
						Utils.p.cluCombo.setSelectedIndex(0);
						break;
					}
					}
					break;
				}
				}

			} catch (Exception e) {
				e.printStackTrace();
				// System.out.println(e.getMessage());
				// run = false;
			}

		}

	}
}
